package com.xiaobao.coalproperty.other.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseFragment<T extends BaseActivity> extends Fragment {

    private T activity;
    private Unbinder mUnbinder;
    private LoadingDailog.Builder mBuilder;
    private LoadingDailog mLoadingDailog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = (T) getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(getContentView(), null, false);
        mUnbinder = ButterKnife.bind(this, view);
        ButterKnife.bind(view);
        init(savedInstanceState);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    protected abstract int getContentView();

    protected abstract void init(Bundle savedInstanceState);

    protected T getSuperActivity() {
        return activity;
    }

    protected void showToast(String message) {
        if (activity == null) {
            return;
        }
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
    }

    protected void showProgress() {
        mBuilder.setMessage("加载中");
        mBuilder.setCancelable(true);
        mLoadingDailog = mBuilder.create();
        mLoadingDailog.show();
    }

    protected void showProgress(String message) {
        mBuilder.setMessage(message);
        mBuilder.setCancelable(true);
        mLoadingDailog = mBuilder.create();
        mLoadingDailog.show();
    }

    protected void hideProgress() {
        if (mLoadingDailog == null || mBuilder == null) {
            return;
        }
        if (mLoadingDailog.isShowing()) {
            mLoadingDailog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}
